package com.mnemonic.views.field;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Image;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import com.mnemonic.player.Player;
import com.mnemonic.player.RobotPlayer;
import com.mnemonic.views.MainPage;

/**
 * 玩家区域
 * 
 * @author lifeng
 * @date 2021/05/13
 */
public class PlayerFields extends JPanel {

    private static final long serialVersionUID = 1L;

    private List<PlayerField> players;

    public PlayerFields(List<Player> players) {
        this.setLayout(new FlowLayout(FlowLayout.LEFT));
        List<PlayerField> fields = new ArrayList<>();
        for (Player player : players) {
            PlayerField field = new PlayerField(player);
            fields.add(field);
            this.add(field);
        }
        this.players = fields;
    }

    /**
     * 激活当前的玩家
     * 
     * @param player
     */
    public void active(Player player) {
        for (PlayerField field : players) {
            field.inactive();
            if (field.player.getId().equals(player.getId())) {
                field.active();
            }
        }
    }

    public void update(Player player) {
        for (PlayerField field : players) {
            if (field.player.getId().equals(player.getId())) {
                field.update();
            }
        }
    }

    public class PlayerField extends JPanel {
        private static final long serialVersionUID = 1L;
        Player player;
        Color bg;

        JLabel scores;
        JLabel faults;

        public PlayerField(Player player) {
            this.player = player;
            this.setLayout(null);
            this.setPreferredSize(new Dimension(260, 60));

            // 图像
            JLabel logo_image = new JLabel();
            ImageIcon _logo_image = new ImageIcon(MainPage.class.getResource(player.getImage()));
            _logo_image.setImage(_logo_image.getImage().getScaledInstance(50, 50, Image.SCALE_DEFAULT));
            logo_image.setAlignmentX(Component.CENTER_ALIGNMENT);
            logo_image.setHorizontalAlignment(SwingConstants.CENTER);
            logo_image.setIcon(_logo_image);
            logo_image.setBounds(5, 5, 50, 50);
            this.add(logo_image);

            // 姓名
            JLabel name = new JLabel((player instanceof RobotPlayer ? "【机器人】" : "【普通玩家】") + player.getName());
            name.setBounds(60, 10, 200, 20);
            this.add(name);

            // 等级
            JLabel grade = new JLabel("等级：" + player.getGrade().name());
            grade.setBounds(65, 30, 75, 20);
            this.add(grade);

            // 此局的得分
            scores = new JLabel("得分：" + player.getScores());
            scores.setBounds(140, 30, 65, 20);
            this.add(scores);

            // 此局的失误数
            faults = new JLabel("失误：" + player.getFaults());
            faults.setBounds(205, 30, 65, 20);
            this.add(faults);

            // 设置边框
            this.setBorder(BorderFactory.createRaisedBevelBorder());

            // 原始背景
            this.bg = this.getBackground();
        }

        public void inactive() {
            this.setBackground(this.bg);
            this.repaint();
            this.revalidate();
            this.updateUI();
        }

        public void active() {
            this.setBackground(Color.RED);
            this.repaint();
            this.revalidate();
            this.updateUI();
        }

        public void update() {
            this.scores.setText("得分：" + player.getScores());
            this.faults.setText("失误：" + player.getFaults());
        }
    }
}
